package com.shwread.android.ui.dialog;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.NewActionHelp;
import com.ciko.code.bean.UserBean;
import com.shwread.android.qysw.activity.BookShelfActivity;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.ui.page.NewEnterprisePage;
import com.TrainActionHelp;

import logic.constants.Const;
import logic.dao.UserDao;


public class NetworkDialog
        extends
        CommonDialog {

    private Activity activity;
    private String account;
    private boolean readBook = false;

    public NetworkDialog(Context context) {
        super(context);

        activity = (Activity) context;
        tvOk.setText("设置网络");
        tvText.setText("网络未连接，请检查网络设置");
    }
    public NetworkDialog(Context context,String account) {//显示离线阅读提示
        this(context);
        vCancel.setText("离线看书");
        readBook = true;
        this.account = account;
    }

    @Override
    protected void clickOk(View v) {
        super.clickOk(v);
        startWirelessSettingsActivity(activity);

        //		activity.finish();
    }

    @Override
    protected void clickCancel(View v) {
        if (readBook) {//离线阅读进入个人书架界面
//            UserBean bean = UserDao.getInstance(activity).getLastLoginUser();
            UserBean bean = UserDao.getInstance(activity).getUser(account);
            if (bean != null) {
            Const.phone_number = bean.getAccount();
            Const.login_key = bean.getKey();
            NewActionHelp.setLoginKey(bean.getKey());
            NewActionHelp.setAccount(bean.getAccount());
            TrainActionHelp.setLoginKey(bean.getKey());
            TrainActionHelp.setAccount(bean.getAccount());
            }
            NewEnterprisePage.netAvilible = false;
//        Intent intent = new Intent(activity, MainActivity.class);
//        intent.setAction(MainActivity.TO_BOOKSHELF_ACTION);
//        activity.startActivity(intent);
            Intent intent = new Intent(activity, BookShelfActivity.class);
            BookShelfActivity.OFFINE_READ = true;
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent);

//        activity.finish();
        }
        super.clickCancel(v);
        //activity.finish();
    }

    public static void startWirelessSettingsActivity(Activity context) {
        try {
            Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {

        }
    }
}
